import 'package:yp_object_mapper/mapper.dart';
import 'package:test/test.dart';

import 'entity.dart';

void main() {
  group('A group of Converter tests', () {
    final converter = ObjectConverter();

    setUp(() {
      ObjectConverter().quickMapperBuilder(builder: (type) => Message());
    });

    test('Decode Test', () async {
      expect(await converter.decode<bool>("1"), true);
      expect(await converter.decode<List<bool>>('[1, "t", 0]'), [true, true, false]);
      expect(await converter.decode<Set<bool>>('[1, "t", 0]'), {true, false});
      expect(await converter.decode<int>("1"), 1);
      expect(await converter.decode<List<int>>('[1, "2", 3]'), [1, 2, 3]);
      expect(await converter.decode<Set<int>>('[1, "2", 3]'), {1, 2, 3});
      // expect(await converter.decode<List<List<int>>>('[[1, "2", 3], [1, "2", 3]]'), [
      //   [1, 2, 3],
      //   [1, 2, 3]
      // ]);
      expect(await converter.decode<double>("1"), 1.0);
      expect(await converter.decode<List<double>>('[1, "2", 3.5]'), [1, 2, 3.5]);
      expect(await converter.decode<Set<double>>('[1, "2", 3.5]'), {1, 2, 3.5});
      expect(await converter.decode<String>("1"), "1");
    });

    test('Map Test', () async {
      expect(await converter.mapWith<bool>(true), true);
      expect(await converter.mapWith<bool>(false), false);
      expect(await converter.mapWith<bool>(null), null);
      expect(await converter.mapWith<bool>("1"), true);
      expect(await converter.mapWith<bool>(1), true);
      expect(await converter.mapWith<List<bool>>([1, "t", 0]), [true, true, false]);
      expect(await converter.mapWith<int>("1"), 1);
      expect(await converter.mapWith<List<int>>([1, "2", 3]), [1, 2, 3]);
      expect(await converter.mapWith<double>("1"), 1.0);
      expect(await converter.mapWith<List<double>>([1, "2", 3.5]), [1, 2, 3.5]);
      expect(await converter.mapWith<String>("1"), "1");
    });

    test('Decode Message Test', () async {
      var json = '''
{"code":14,"op":8,"sop":6,"sp":"1/scene.query","wopid":414,"data":"Operation Pending"}
''';
      Message? obj = await converter.decode(json);
      expect(obj?.code, 14);
      expect(obj?.wopid, 414);
      expect(obj?.op, 8);
      expect(obj?.sop, 6);
      expect(obj?.sp, "1/scene.query");
      expect(obj?.data, "Operation Pending");
    });

    test('Encode Message Test', () async {
      var obj = Message(
        op: 8,
        sop: 6,
        wopid: 414,
        code: 14,
        sp: "1/scene.query",
        data: "Operation Pending",
      );
      var json = await converter.encode(obj);
      expect(json, '{"code":14,"op":8,"sop":6,"wopid":414,"sp":"1/scene.query","data":"Operation Pending"}');
    });

    test('Decode Message List Test', () async {
      var json = '''
[{"wopid":414,"op":8,"sop":6,"code":14,"sp":"1/scene.query","data":"Operation Pending"},{"opid":100,"wopid":415,"op":7,"sop":1,"code":14,"sp":"1/scene.query","data":"Operation Pending"}]
''';
      List<Message>? obj = await converter.decode(json);
      expect(obj?.length, 2);
      expect(obj?[0].code, 14);
      expect(obj?[0].wopid, 414);
      expect(obj?[0].op, 8);
      expect(obj?[0].sop, 6);
      expect(obj?[0].sp, "1/scene.query");
      expect(obj?[0].data, "Operation Pending");
      expect(obj?[1].code, 14);
      expect(obj?[1].wopid, 415);
      expect(obj?[1].op, 7);
      expect(obj?[1].sop, 1);
      expect(obj?[1].sp, "1/scene.query");
      expect(obj?[1].data, "Operation Pending");
      expect(obj?[1].opid, 100);
    });

    test('Encode Message List Test', () async {
      var obj = [
        Message(
          op: 8,
          sop: 6,
          wopid: 414,
          code: 14,
          sp: "1/scene.query",
          data: "Operation Pending",
        ),
        Message(
          op: 7,
          sop: 1,
          opid: 100,
          wopid: 415,
          code: 14,
          sp: "1/scene.query",
          data: "Operation Pending",
        ),
      ];
      var json = await converter.encode(obj);
      expect(json,
          '[{"code":14,"op":8,"sop":6,"wopid":414,"sp":"1/scene.query","data":"Operation Pending"},{"code":14,"op":7,"sop":1,"opid":100,"wopid":415,"sp":"1/scene.query","data":"Operation Pending"}]');
    });

    test('Convert Message Test', () async {
      var obj = Message();
      obj.wopid = 414;
      obj.op = 8;
      obj.code = 14;
      obj.sop = 6;
      obj.sp = "1/scene.query";
      obj.data = "Operation Pending";
      Message conv = await converter.convert(obj);
      expect(identical(conv, obj), isFalse);
      expect(conv.code, 14);
      expect(conv.wopid, 414);
      expect(conv.op, 8);
      expect(conv.sop, 6);
      expect(conv.sp, "1/scene.query");
      expect(conv.data, "Operation Pending");
    });
  });
}
